package com.salim.leetcode.$101;

import com.salim.leetcode.util.TreeNode;
import com.salim.leetcode.util.TreeNodeUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * https://leetcode.com/problems/symmetric-tree/
 */
@Slf4j
public class SymmetricTree1 {

    /**
     * 思路1 读完比较 这种比较慢
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        StringBuilder left = new StringBuilder();
        StringBuilder right = new StringBuilder();
        read(root.left,left);
        swapRead(root.right,right);
        if(left.toString().equals(right.toString())){
            return true;
        }else{
            return false;
        }
    }

    private void read(TreeNode current,StringBuilder stringBuilder){
        if(current==null){
            stringBuilder.append(" ");
            return;
        }
        stringBuilder.append(current.val);
        read(current.left,stringBuilder);
        read(current.right,stringBuilder);
    }
    private void swapRead(TreeNode current,StringBuilder stringBuilder){
        if(current==null){
            stringBuilder.append(" ");
            return;
        }
        stringBuilder.append(current.val);
        swapRead(current.right,stringBuilder);
        swapRead(current.left,stringBuilder);
    }

    public static void main(String[] args){
        Integer[] arr = {2,3,3,4,5,5,4,null,null,8,9,9,8};
        TreeNode root = TreeNodeUtils.generateTreeNode(arr);
        SymmetricTree1 tree = new SymmetricTree1();
        log.info("{2,3,3,4,5,5,4,null,null,8,9,9,8},{}",tree.isSymmetric(root));
        Integer[] arr2 = {1,2,2,3,4,4,3};
        root = TreeNodeUtils.generateTreeNode(arr2);
        log.info("{1,2,2,3,4,4,3},{}",tree.isSymmetric(root));
        Integer[] arr3 = {1,2,2,null,3,null,3};
        root = TreeNodeUtils.generateTreeNode(arr3);
        log.info("{1,2,2,null,3,null,3},{}",tree.isSymmetric(root));
    }
}

